package com.zsj.comminlibrary.util;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.zsj.comminlibrary.R;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

public class SpManager {
    private static SpManager sSpManager;
    private Context mContext;
    private Map<String, Object> mMap;
    private String mCacheDir;
    private SpManager() {
    }

    public static SpManager getInstance() {
        if (sSpManager == null) {
            sSpManager = new SpManager();
        }
        return sSpManager;
    }

    public void init(Context context) {
        this.mContext = context;
        String path = Environment.getExternalStorageDirectory() + "/smartPad";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        mCacheDir = path;
        mMap = new HashMap<>();
    }

    public String getBgName() {
        Object bg = getObject("bg");
        if (bg == null) {
            return null;
        }
        return (String) bg;
    }

    public int getBgId() {
        Object bg = getObject("bg");
        if (bg == null) {
            return R.mipmap.bg;
        }
        String bgName = (String) bg;
        if (StringUtils.isEmpty(bgName)) {
            return -1;
        }
        if ("bg".equals(bgName)) {
            return R.mipmap.bg;
        } else if ("bg1".equals(bgName)) {
            return R.mipmap.bg1;
        } else if ("bg2".equals(bgName)) {
            return R.mipmap.bg2;
        } else if ("bg3".equals(bgName)) {
            return R.mipmap.bg3;
        } else if ("bg4".equals(bgName)) {
            return R.mipmap.bg4;
        } else if ("bg5".equals(bgName)) {
            return R.mipmap.bg5;
        } else if ("bg6".equals(bgName)) {
            return R.mipmap.bg6;
        }else if ("bg7".equals(bgName)) {
            return R.mipmap.bg7;
        } else if ("bg8".equals(bgName)) {
            return R.mipmap.bg8;
        } else if ("bg9".equals(bgName)) {
            return R.mipmap.bg9;
        } else if ("bg10".equals(bgName)) {
            return R.mipmap.bg10;
        } else if ("bg11".equals(bgName)) {
            return R.mipmap.bg11;
        } else if ("bg12".equals(bgName)) {
            return R.mipmap.bg12;
        }else if ("bg13".equals(bgName)) {
            return R.mipmap.bg13;
        }

        return -1;
    }

    public void putBg(String bg) {
        putObject("bg", bg);
    }

    public String getString(String key) {
        Object object = getObject(key);
        if (object == null) {
            return null;
        } else {
            return (String) object;
        }
    }


    public void putString(String key, String value) {
        putObject(key, value);
    }


    public String getSn() {
        String sn = getString("sn");
        return sn;
    }

    public void putSn(String sn) {
        putString("sn", sn);
    }

    public String getDevId() {
        String devId = getString("devId");
        return devId;
    }

    public String getDevToken() {
        return getString("devToken");
    }

    public void putDevId(String devId) {
        putString("devId", devId);
    }

    public void putDevToken(String token) {
        putString("devToken", token);
    }

    public void putDeviceInfo(GetDeviceInfoResp resp) {
        putString("deviceInfo", new Gson().toJson(resp));
    }

    public GetDeviceInfoResp getDeviceInfo() {
        String deviceInfo = getString("deviceInfo");
        if (deviceInfo == null || TextUtils.isEmpty(deviceInfo)) {
            return null;
        }
        GetDeviceInfoResp getDeviceInfoResp = new Gson().fromJson(deviceInfo, GetDeviceInfoResp.class);
        return getDeviceInfoResp;
    }

    public  Object getObject(String key) {
        if (key != null) {
            if (mMap.keySet().contains(key)) {
                Object o = mMap.get(key);
                if (o != null) {
                    return o;
                }
            }
            String fileName = mCacheDir + File.separator + key;
            return readObject(fileName);
        }
        return null;
    }

    private  Object readObject(String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            return null;
        }

        Object obj = null;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(fileName);
            bis = new BufferedInputStream(fis);
            ois = new ObjectInputStream(bis);
            obj = ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
                if (bis != null)
                    bis.close();
                if (ois != null)
                    ois.close();
            } catch (IOException e) {
            }
        }
        return obj;
    }

    private  void writeObject(String fileName, Object object) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(fileName);
            bos = new BufferedOutputStream(fos);
            oos = new ObjectOutputStream(bos);
            oos.writeObject(object);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null)
                    oos.close();
                if (bos != null)
                    bos.close();
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
            }
        }
    }

    public  void putObject(String key, Object object) {
        if (key == null) {
            return;
        }
        mMap.put(key, object);
        String fileName = mCacheDir + File.separator + key;
        writeObject(fileName, object);
    }
}
